<?php
class Blog{
	
	function AdminList($mysql,$offset){
		$mysql->query("select * from blog");
		$max = $mysql->numrows();
		
		$mysql->query("select * from blog order by timestamp desc limit 3 offset $offset");
		echo "<table>";
		echo "<tr><td align='center'><b>Title</b></td><td align='center'><b>Content</b></td><td align='center'><b>Author</b></td></tr>";
		while($entry = $mysql->fetcharray()){
			echo '<tr><td style="cursor:pointer" onclick="LoadBlogEntry(\''.$entry['title'].'\','.$entry['id'].')">'.$entry['title'].'</td><td>'.substr($entry['content'],0,100).'...</td><td>'.$entry['author'].'</td></tr>';
		}
		echo "</table><br />";
		if($offset > 0){ echo "<font class=\"button\" onclick=\"AdminBlogList(".($offset-3).")\">Previous</font>&nbsp;&nbsp;&nbsp;"; }
		if(($offset+3) <= $max){ echo "<font class=\"button\" onclick=\"AdminBlogList(".($offset+3).")\">Next</font>"; }
		echo "<br /><br /><font class=\"button\" onclick=\"RestoreNewPostScreen()\"><b>Add New</b></font>";
	}
	function AdminSingle($mysql,$id){
		$mysql->query("select content from blog where id = $id");
		while($entry = $mysql->fetcharray()){
			echo $entry['content'];
			break;
		}
	}
	
	function Read($mysql,$id){
		$topost = ""; $title = "";
		$mysql->query("select * from blog where id = $id");
		
		while($entry = $mysql->fetcharray()){
			$timestamp = date("jS F Y",$entry['timestamp']);
			$timestamp .= " at " . date("g:ia",$entry['timestamp']);
			$content = $entry['content'];
			$title = $entry['title'];
			$code = $entry['id'];
			$topost = "<table><tr><td><a href=\"index.php?b=$code\"><h2>$title</h2></a></td><td valign=\"bottom\" style=\"padding-bottom:3px\">&nbsp;&nbsp;<small>$timestamp</small></td></tr></table><p>$content</p>";
			break;
		}
		return array($title,$topost);
	}
	
	function PostList($mysql,$limit,$offset){
		$topost = "";
		$mysql->query("select * from blog");
		$max = $mysql->numrows();
		$mysql->query("select * from blog order by timestamp desc limit $limit offset $offset");
		while($entry = $mysql->fetcharray()){
			$timestamp = date("jS F Y",$entry['timestamp']);
			$timestamp .= " at " . date("g:ia",$entry['timestamp']);
			$code = $entry['id'];
			$content = substr($entry['content'],0,250) . "... <a href=\"index.php?b=$code\" style=\"float:right\"><u><b>Read More</b></u></a><br /><br />";
			$title = $entry['title'];
			$topost .= "<table><tr><td valign=\"bottom\"><a href=\"index.php?b=$code\"><h2>$title</h2></a></td><td valign=\"bottom\" style=\"padding-bottom:3px\">&nbsp;&nbsp;<small>$timestamp</small></td></tr></table><p>$content</p><hr>";
		}
		
		$toreturn = substr($topost,0,strlen($topost)-4);
		if($offset > 0){ $toreturn .= "<br /><a style=\"float:left\" href=\"index.php?bp=".($offset-$limit)."\">Newer Posts</a>"; }
		if(($offset+$limit) <= $max){ $toreturn .= "<br /><a style=\"float:right\" href=\"index.php?bp=".($offset+$limit)."\">Older Posts</a>"; }
		return $toreturn;
	}
	
	function NewEntry($mysql,$title,$content,$author){
		$timestamp = time();
		$title = mysql_escape_string($title);
		$content = mysql_escape_string($content);
		$mysql->query("insert into blog (title,content,timestamp,author)values('$title','$content','$timestamp','$author')");
	}
	function SaveEntry($mysql,$id,$title,$content){
		$title = mysql_escape_string($title);
		$content = mysql_escape_string($content);
		$mysql->query("update blog set title='$title', content='$content' where id = $id");
	}
	function DeleteEntry($mysql,$id){
		$id = mysql_escape_string($id);
		$mysql->query("delete from blog where id = $id");
	}

}
?>